package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;

import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @description: TOOD
 * @return:
 * @author: BaoZi
 * @date: 2022/9/20
 */
@Service
public class ICartServiceImpl implements ICartService {

    @Autowired
    RedissonClient redissonClient;
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    CartItemConverter cartItemConverter;

    @Override
    public List<CartProductDto> getCartListById(Long userId) {
        Map<Long, CartProductDto> cartMap = redissonClient.getMap(userId.toString());

        List<CartProductDto> list = new ArrayList<>();
        for (Long uid : cartMap.keySet()) {
            CartProductDto cartProductDto = cartMap.get(uid);
            list.add(cartProductDto);
        }
        return list;
    }

    @Override
    public void addToCart(CartRequest request) {
        Map<Long, CartProductDto> map = redissonClient.getMap(request.getUserId().toString());
        //判断购物车中是否已有此商品
        CartProductDto cartProductDto1 = map.get(request.getProductId());
        if (cartProductDto1!=null){
            //已经有此商品，修改数量和价钱
            Long productNum = cartProductDto1.getProductNum();
            long znum = ((long) request.getProductNum()) + productNum;
            cartProductDto1.setProductNum(znum);
            map.put(request.getProductId(),cartProductDto1);
            return;
        }
        //添加新商品到购物车
        Item item = itemMapper.selectByPrimaryKey(request.getProductId());
        CartProductDto cartProductDto = cartItemConverter.item2Dto(item);
        cartProductDto.setChecked("true");
        cartProductDto.setProductNum(((long) request.getProductNum()));
        map.put(request.getProductId(),cartProductDto);

    }

    @Override
    public void updateCartNum(CartRequest request) {

        Map<Long, CartProductDto> map = redissonClient.getMap(request.getUserId().toString());
        CartProductDto cart = map.get(request.getProductId());
        cart.setProductNum(((long) request.getProductNum()));
        cart.setChecked(request.getChecked());
        map.put(request.getProductId(),cart);
    }

    @Override
    public void checkAllCartItem(CheckAllItemRequest request) {
        // 根据uid获取购物车
        Map<Long, CartProductDto> cart = redissonClient.getMap(request.getUserId().toString());
        // 遍历购物车中的商品，改变checked状态
        for (Long key : cart.keySet()) {
            CartProductDto cartProductDto = cart.get(key);
            cartProductDto.setChecked(request.getChecked());
            cart.put(key, cartProductDto);
        }
    }

    @Override
    public void deleteCartItem(Long uid, Long itemId) {
        // 获取购物车
        Map<Long, CartProductDto> cart = redissonClient.getMap(uid.toString());
        // 删除商品
        cart.remove(itemId);
    }

    @Override
    public void deleteCheckedItem(Long userId) {
        // 获取购物车
        Map<Long, CartProductDto> cart = redissonClient.getMap(userId.toString());
        //删除选中的商品
        for (Long itemId : cart.keySet()) {
            CartProductDto cartProductDto = cart.get(itemId);
            if ("true".equals(cartProductDto.getChecked())) {
                cart.remove(itemId);
            }
        }
    }
}
